package com.example.sqliteviewer;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class TableInfoUtils {

    /**
     * 获取数据表中各字段的名称及类型信息
     *
     * @param database SQLite数据库实例
     * @param tableName 表名
     * @return 包含字段信息的列表，每个字段信息为ColumnInfo对象
     */
    public static List<ColumnInfo> getTableColumnInfo(SQLiteDatabase database, String tableName) {
        List<ColumnInfo> columnInfoList = new ArrayList<>();
        
        try {
            // 使用PRAGMA table_info命令获取表结构信息
            Cursor cursor = database.rawQuery("PRAGMA table_info(" + tableName + ")", null);
            
            if (cursor.moveToFirst()) {
                do {
                    ColumnInfo columnInfo = new ColumnInfo();
                    
                    // cid: 列序号
                    columnInfo.setCid(cursor.getString(cursor.getColumnIndex("cid")));
                    // name: 列名
                    columnInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
                    // type: 数据类型
                    columnInfo.setType(cursor.getString(cursor.getColumnIndex("type")));
                    // notnull: 是否可为空 (1表示不可为空，0表示可为空)
                    columnInfo.setNotNull(cursor.getString(cursor.getColumnIndex("notnull")));
                    // dflt_value: 默认值
                    columnInfo.setDfltValue(cursor.getString(cursor.getColumnIndex("dflt_value")));
                    // pk: 是否为主键 (1表示是主键，0表示不是)
                    columnInfo.setPk(cursor.getString(cursor.getColumnIndex("pk")));
                    
                    columnInfoList.add(columnInfo);
                } while (cursor.moveToNext());
                
                cursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return columnInfoList;
    }
    
    /**
     * 获取表的所有索引信息
     *
     * @param database SQLite数据库实例
     * @param tableName 表名
     * @return 包含索引信息的列表，每个索引信息为IndexInfo对象
     */
    public static List<IndexInfo> getTableIndexInfo(SQLiteDatabase database, String tableName) {
        List<IndexInfo> indexInfoList = new ArrayList<>();
        
        try {
            // 使用PRAGMA index_list命令获取索引信息
            Cursor cursor = database.rawQuery("PRAGMA index_list(" + tableName + ")", null);
            
            if (cursor.moveToFirst()) {
                do {
                    IndexInfo indexInfo = new IndexInfo();
                    
                    // name: 索引名称
                    indexInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
                    // unique: 是否唯一索引
                    indexInfo.setUnique(cursor.getString(cursor.getColumnIndex("unique")));
                    
                    indexInfoList.add(indexInfo);
                } while (cursor.moveToNext());
                
                cursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return indexInfoList;
    }
}